
const Mock = require('mockjs');
Mock.setup({
  timeout: Math.floor(Math.random() * 600)
});
let configArray = [];

// 使用webpack的require.context()遍历所有mock文件
const files = require.context('.', true, /\.js$/);
files.keys().forEach((key) => {
  if (key === './index.js' || key.includes('data.js')) return;
  configArray = configArray.concat(files(key).default);
});

// 注册所有的mock服务
configArray.forEach((item) => {
  // eslint-disable-next-line no-unused-vars
  for (const [path, target] of Object.entries(item)) {
    const protocol = path.split('|');
    const targetValue = Object.entries(target);
    const data = {};
    for (const [key, value] of targetValue) {
      data[key] = value;
    }
    Mock.mock(protocol[1], protocol[0], {
      data,
      code: 200
    });
  }
});
